package com.zero.manager_following.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zero.manager_following.common.TokenUtils;
import com.zero.manager_following.entity.User;
import com.zero.manager_following.exception.ServiceException;
import com.zero.manager_following.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 功能：业务层的处理
 * 作者：zero
 * 日期：2024/9/13 13:29
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    @Resource
    private UserMapper userMapper;

    /*
    public User selectByUsername(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //where username = #{username}
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }
    */

    @Override
    public boolean save(User entity) {
        //判断用户名是否重复
        if (selectByUsername(entity.getUsername()) != null) {
            throw new ServiceException("该用户名已存在，请更换其他用户名");
        }
        //设置默认用户名和密码
        if(StrUtil.isBlank(entity.getName())){
            entity.setName(entity.getUsername());
        }
        if(StrUtil.isBlank(entity.getPassword())){
            entity.setPassword("00000000");
        }
        if(StrUtil.isBlank(entity.getRole())){
            entity.setRole("USER");
        }
        return super.save(entity);
    }
    public User selectByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return this.getOne(queryWrapper);
    }

    public User login(String username, String password) {
        User dbUser = this.selectByUsername(username);
        if (dbUser == null) {
            //弹出一个自定义异常
            throw new ServiceException("不存在该用户，请更换其他用户名");
        }
        if (!Objects.equals(dbUser.getPassword(), password)) {
            throw new ServiceException("用户名或密码错误");
        }
        //生成token
        String token = TokenUtils.generateToken(dbUser.getId().toString(), dbUser.getPassword());
        //设置token
        dbUser.setToken(token);
        return dbUser;
    }

    public void register(User user) {
        if (selectByUsername(user.getUsername()) != null) {
            throw new ServiceException("该用户名已存在，请更换其他用户名");
        }
        user.setName(user.getUsername());
        save(user);
    }

    public void updatePwd(User user) {
        if(StrUtil.isEmpty(user.getPhone()) && StrUtil.isEmpty(user.getName())){
            throw new ServiceException("用户名或手机号不能为空");
        }
        User dbuser = selectByUsername(user.getUsername());
        if(dbuser == null){
            throw new ServiceException("用户不存在");
        }
        if(StrUtil.equals(user.getPhone(), dbuser.getPhone())){
            dbuser.setPassword("000000");
        }else{
            throw new ServiceException("用户的手机号错误");
        }
        updateById(user);
    }

    public void saveByNoId(User user) {
        userMapper.saveByNoId(user);
    }
}
